﻿<!-- Views/Employee/ExamResult.cshtml -->
@model WebApplication1.Models.ExamResult

@{
    ViewBag.Title = WebApplication1.Utilities.LanguageManager.GetText("ExamResult") + " - " + WebApplication1.Utilities.LanguageManager.GetText("ExamTitle");
}

<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="page-header mb-4">
                <h2 class="text-primary">
                    <i class="glyphicon glyphicon-list-alt"></i>
                    @ViewBag.ExamResult - @Model.Exam.ExamName
                </h2>
            </div>
        </div>
    </div>

    <!-- 考试基本信息 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card border-primary">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">
                        <i class="glyphicon glyphicon-info-sign me-2"></i>
                        @ViewBag.ExamInfo
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <p>
                                <strong>@ViewBag.UserName:</strong>
                                <span class="badge bg-info">@Model.User.FullName</span>
                            </p>
                            <p>
                                <strong>@ViewBag.StartTime:</strong>
                                <span class="badge bg-warning">@Model.StartTime.ToString("yyyy-MM-dd HH:mm:ss")</span>
                            </p>
                        </div>
                        <div class="col-md-6">
                            <p>
                                <strong>@ViewBag.EndTime:</strong>
                                <span class="badge bg-warning">@(Model.EndTime.ToString("yyyy-MM-dd HH:mm:ss") ?? ViewBag.NotCompleted)</span>
                            </p>
                            @{
                                string durationStr = "0.00";
                                if (Model.EndTime != null && Model.StartTime != null)
                                {
                                    try
                                    {
                                        // 新的计算方法：使用 TotalMinutes 属性直接获取分钟数
                                        TimeSpan durationSpan = ((DateTime)Model.EndTime).Subtract((DateTime)Model.StartTime);
                                        durationStr = durationSpan.TotalMinutes.ToString("F2");
                                    }
                                    catch
                                    {
                                        durationStr = "0.00";
                                    }
                                    ViewBag.durationStr = durationStr;
                                }
                            }

                            <p>
                                <strong>@ViewBag.TimeUsed:</strong>
                                <span class="badge bg-success">@ViewBag.durationStr  @WebApplication1.Utilities.LanguageManager.GetText("Minutes")</span>
                            </p>
                        </div>
                    </div>
                    <div class="row mt-3">
                        <div class="col-12">
                            <div class="d-flex justify-content-center">
                                <div class="alert alert-@(ViewBag.fenshu >= Model.TotalScore * 0.6 ? "success" : "danger") text-center w-50">
                                    <h4>
                                        @ViewBag.TotalScore:
                                        <strong>@ViewBag.fenshu</strong> / @Model.TotalScore @ViewBag.Score
                                    </h4>
                                    @{
                                        var percentage = Model.TotalScore > 0 ? (double)ViewBag.fenshu / Model.TotalScore * 100 : 0;
                                    }
                                    <p>
                                        @ViewBag.Percentage:
                                        <strong>@percentage.ToString("F1")%</strong>
                                        <span class="badge @(percentage >= 60 ? "bg-success" : "bg-danger")">
                                            @(percentage >= 60 ? ViewBag.Pass : ViewBag.Fail)
                                        </span>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 答题详情 -->
    <div class="row">
        <div class="col-12">
            <div class="card border-info">
                <div class="card-header bg-info text-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            <i class="glyphicon glyphicon-list me-2"></i>
                            @WebApplication1.Utilities.LanguageManager.GetText("QuestionDetails")
                        </h5>
                        <span class="badge bg-light text-dark">
                            @WebApplication1.Utilities.LanguageManager.GetText("Total"): @Model.Exam.Questions.Where(x=>x.IsActive).ToList().Count @WebApplication1.Utilities.LanguageManager.GetText("Questions")
                        </span>
                    </div>
                </div>
                <div class="card-body">
                    @if (Model.UserAnswers != null && Model.UserAnswers.Any())
                    {
                        <div class="accordion" id="questionsAccordion">
                            @{
                                int index = 1;
                                var orderedQuestions = Model.Exam.Questions.Where(x => x.IsActive).ToList().OrderBy(q => q.QuestionId).ToList();
                            }
                            @foreach (var question in orderedQuestions)
                            {
                                var userAnswer = Model.UserAnswers.FirstOrDefault(ua => ua.QuestionId == question.QuestionId);
                                var isCorrect = userAnswer != null && userAnswer.IsCorrect;

                                <div class="accordion-item mb-3 border rounded">
                                    <h2 class="accordion-header" id="heading@(index)">
                                        <button class="accordion-button @(index == 1 ? "" : "collapsed") p-3 @(isCorrect ? "bg-success-light" : "bg-danger-light")"
                                                type="button"
                                                data-bs-toggle="collapse"
                                                data-bs-target="#collapse@(index)"
                                                aria-expanded="@(index == 1 ? "true" : "false")"
                                                aria-controls="collapse@(index)">
                                            <div class="d-flex align-items-center w-100">
                                                <div class="me-3">
                                                    <span class="badge @(isCorrect ? "bg-success" : "bg-danger") rounded-pill">
                                                        @index
                                                    </span>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <div class="fw-bold">
                                                        @question.QuestionText
                                                    </div>
                                                    <div class="small text-muted mt-1">
                                                        <span class="badge bg-primary me-2">@question.Score @ViewBag.Score</span>
                                                        @{
                                                            string typeText = "";
                                                            switch (question.QuestionType)
                                                            {
                                                                case "Single":
                                                                    typeText = ViewBag.SingleChoice;
                                                                    break;
                                                                case "Multiple":
                                                                    typeText = ViewBag.MultipleChoice;
                                                                    break;
                                                                case "TrueFalse":
                                                                    typeText = ViewBag.TrueFalse;
                                                                    break;
                                                                case "Fill":
                                                                    typeText = ViewBag.FillBlank;
                                                                    break;
                                                                default:
                                                                    typeText = question.QuestionType;
                                                                    break;
                                                            }
                                                        }
                                                        <span class="badge bg-secondary">@typeText</span>
                                                    </div>
                                                </div>
                                                <div class="ms-3">
                                                    @if (isCorrect)
                                                    {
                                                        <span class="badge bg-success">
                                                            <i class="glyphicon glyphicon-ok me-1"></i>@ViewBag.TrueValue
                                                        </span>
                                                    }
                                                    else
                                                    {
                                                        <span class="badge bg-danger">
                                                            <i class="glyphicon glyphicon-remove me-1"></i>@ViewBag.FalseValue
                                                        </span>
                                                    }
                                                </div>
                                            </div>
                                        </button>
                                    </h2>
                                    <div id="collapse@(index)" class="accordion-collapse collapse @(index == 1 ? "show" : "")"
                                         aria-labelledby="heading@(index)"
                                         data-bs-parent="#questionsAccordion">
                                        <div class="accordion-body">
                                            <div class="row">
                                                <!-- 题目选项 -->
                                                <div class="col-md-6">
                                                    <h6 class="text-primary">
                                                        <i class="glyphicon glyphicon-question-sign me-2"></i>
                                                        @WebApplication1.Utilities.LanguageManager.GetText("Options")
                                                    </h6>
                                                    <div class="bg-light p-3 rounded mb-3">
                                                        @if (question.QuestionType == "Fill")
                                                        {
                                                            <div class="alert alert-info">
                                                                <i class="glyphicon glyphicon-info-sign me-2"></i>
                                                                @WebApplication1.Utilities.LanguageManager.GetText("FillBlankNoOptions")
                                                            </div>
                                                        }
                                                        else if (question.QuestionType == "TrueFalse")
                                                        {
                                                            <div class="d-flex gap-2">
                                                                <div class="border rounded p-2 w-50 text-center">
                                                                    <i class="glyphicon glyphicon-ok-circle text-success me-1"></i>@ViewBag.TrueValue
                                                                </div>
                                                                <div class="border rounded p-2 w-50 text-center">
                                                                    <i class="glyphicon glyphicon-remove-circle text-danger me-1"></i>@ViewBag.FalseValue
                                                                </div>
                                                            </div>
                                                        }
                                                        else
                                                        {
                                                            {
                                                                var options = question.Options.OrderBy(o => o.OptionLabel).ToList();
                                                                ViewBag.options = options;
                                                            }
                                                            <div class="options-container">
                                                                @foreach (var option in ViewBag.options)
                                                                {
                                                                    <div class="border rounded p-2 mb-2">
                                                                        <strong>@option.OptionLabel.</strong> @option.OptionText
                                                                    </div>
                                                                }
                                                            </div>
                                                        }
                                                    </div>
                                                </div>

                                                <!-- 答案对比 -->
                                                <div class="col-md-6">
                                                    <h6 class="text-success">
                                                        <i class="glyphicon glyphicon-check me-2"></i>@WebApplication1.Utilities.LanguageManager.GetText("AnswerComparison")
                                                    </h6>
                                                    <div class="mb-3">
                                                        <div class="bg-info bg-opacity-10 p-3 rounded">
                                                            <strong>@ViewBag.YourAnswer:</strong>
                                                            <div class="mt-2">
                                                                @if (userAnswer == null || string.IsNullOrEmpty(userAnswer.Answer))
                                                                {
                                                                    <span class="badge bg-secondary">@WebApplication1.Utilities.LanguageManager.GetText("NotAnswered")</span>
                                                                }
                                                                else
                                                                {
                                                                    var userAnswerLabels = userAnswer.Answer.Split(',');
                                                                    foreach (var label in userAnswerLabels)
                                                                    {
                                                                        <span class="badge bg-primary me-1">@label</span>
                                                                    }
                                                                }
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="mb-3">
                                                        <div class="bg-success bg-opacity-10 p-3 rounded">
                                                            <strong>@ViewBag.CorrectAnswer:</strong>
                                                            <div class="mt-2">
                                                                @{
                                                                    var correctAnswer = userAnswer.CorrectAnswer ?? question.CorrectAnswer;
                                                                }
                                                                @if (string.IsNullOrEmpty(correctAnswer))
                                                                {
                                                                    <span class="badge bg-secondary">@WebApplication1.Utilities.LanguageManager.GetText("None")</span>
                                                                }
                                                                else
                                                                {
                                                                    var correctAnswerLabels = correctAnswer.Split(',');
                                                                    foreach (var label in correctAnswerLabels)
                                                                    {
                                                                        <span class="badge bg-success me-1">@label</span>
                                                                    }
                                                                }
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div>
                                                        <div class="bg-@(isCorrect ? "success" : "danger") bg-opacity-10 p-3 rounded">
                                                            <strong>@ViewBag.Score:</strong>
                                                            <span class="badge bg-@(isCorrect ? "success" : "danger")">
                                                                @((userAnswer != null) ? userAnswer.Score : 0) @ViewBag.Score
                                                            </span>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                index++;
                            }
                        </div>
                    }
                    else
                    {
                        <div class="alert alert-warning text-center">
                            <i class="glyphicon glyphicon-info-sign me-2"></i>
                            @WebApplication1.Utilities.LanguageManager.GetText("NoAnswerDetails")
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="row mt-4">
        <div class="col-12 text-center">
            <a href="@Url.Action("Index", "admin")" class="btn btn-primary btn-lg">
                <i class="glyphicon glyphicon-arrow-left me-2"></i>@ViewBag.Back
            </a>
        </div>
    </div>
</div>

<!-- 确保正确的脚本加载顺序 -->
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

<style>
    .card {
        border-radius: 10px;
        transition: transform 0.2s, box-shadow 0.2s;
    }

        .card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.15) !important;
        }

    .bg-success-light {
        background-color: #d4edda !important;
    }

    .bg-danger-light {
        background-color: #f8d7da !important;
    }

    .badge {
        font-weight: 500;
    }

    .btn-lg {
        padding: 0.8rem 1.5rem;
        font-weight: 500;
    }

    .accordion-button:not(.collapsed) {
        background-color: #f8f9fa;
        color: #000;
    }

    .accordion-button:focus {
        box-shadow: none;
        border-color: rgba(0, 0, 0, 0.125);
    }

    .options-container {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    /* 响应式调整 */
    @@media (max-width: 768px) {
        .accordion-button {
            padding: 1rem;
        }

        .btn-lg {
            padding: 0.6rem 1rem;
            font-size: 0.9rem;
        }
    }
</style>

<script>
    $(document).ready(function () {
        // 为手风琴添加平滑滚动
        $('.accordion-button').on('click', function () {
            var target = $($(this).attr('data-bs-target'));
            if (!target.hasClass('show')) {
                $('html, body').animate({
                    scrollTop: target.parent().offset().top - 100
                }, 300);
            }
        });
    });
</script>